package com.ibiaoke.boss.controller;


import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.system.entity.Advert;
import com.ibiaoke.system.service.AdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */
@Controller
public class AdvertController extends BaseController {

    @Autowired
    private AdvertService advertService;

    /**
     * 添加Advert
     *
     * @param advert
     * @return
     */
    @ResponseBody
    @RequestMapping("saveAdvert")
    public Res saveAdvert(@Valid Advert advert, BindingResult result) {
        handleValidate(result);
        advertService.saveAdvert(advert);
        return Res.create();
    }

    /**
     * 修改Advert
     *
     * @param advert
     * @return
     */
    @ResponseBody
    @RequestMapping("updateAdvertById")
    public Res updateAdvertById(Advert advert) {
        advertService.updateBannerById(advert);
        return Res.create();
    }

    /**
     * 删除Advert
     *
     * @param advertId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAdvertById")
    public Res deleteAdvertById(Integer advertId) {
        int deleteBannerById = advertService.deleteBannerById(advertId);
        return Res.create();
    }

    /**
     * 查询Advert
     *
     * @param position
     * @return
     */
    @ResponseBody
    @RequestMapping("listAdvert")
    public Res listAdvert(Integer position, Page page) {
        List<Advert> advertList = advertService.listBanner(position, null, page);
        return Res.create().addResponse("advertList", advertList).addResponse("page", page);
    }
}
